package com.bjpowernode.crm.settings.controller;

import com.bjpowernode.crm.commons.contans.Contans;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.plugin.com.Utils;

import javax.rmi.CORBA.Util;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Controller
@RequestMapping("/settings/qx/user")
public class UserController  extends HttpServlet {

    @Autowired
    private UserService userService;

    /**
     * 用户登录验证
     * @param loginAct
     * @param loginPwd
     * @return
     * @throws IOException
     */
    @RequestMapping("/login.do")
    public @ResponseBody Object login(String loginAct, String loginPwd, HttpSession session){
        HashMap<String,Object > map = new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        System.out.println(map);

        //调用service层查询
        User user = userService.queryUserByLoginActAndPwd(map);

        //返回状态
        ReturnObject returnObject = new ReturnObject();
        //判断是否登陆成功
        if(user == null){
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
            returnObject.setMessage("用户名或密码错误");
        }else{
            //提取系统时间
            String sysTime = DateUtils.formateDateTime(new Date());
            if(sysTime.compareTo(user.getExpireTime())>0){
                //过期时间
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
                returnObject.setMessage("账号已过期");

            }else if ("0".equals(user.getLockState())){
                //账户锁定
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
                returnObject.setMessage("账户被锁定");
            }else{
                    //登录成功
                returnObject.setCode(Contans.RETURN_OBJECT_CODE_SUCCESS);
                //登陆成功后,把user放到session作用域中
                session.setAttribute(Contans.SESSION_USER,user);
            }
        }
        return returnObject;
    }


    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/logout.do")
    public String logout(HttpSession session){
        //销毁session
        session.invalidate();
        //重定向,跳转到首页
        return "redirect:/";
    }

    /**
     * 修改用户密码
     * @param
     * @param
     * @return
     */
    @RequestMapping("/updatePassword.do")
    @ResponseBody
    public ReturnObject updatePassword(String oldPwd, String newPwd, String confirmPwd,HttpSession session) throws IOException {

        HashMap<String, String> map = new HashMap<>();
        //返回状态
        ReturnObject returnObject = new ReturnObject();

        //获取当前用户用户名,和密码
        User user = (User) session.getAttribute(Contans.SESSION_USER);
        String loginAct = user.getLoginAct();
        String loginPwd = user.getLoginPwd();

        //获取输入的密码和修改的密码


        //原密码输入错误
        if( !loginPwd.equals(oldPwd) ){
            //弹窗提醒,您输入的旧密码错误
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_FALSE);
            returnObject.setMessage("您输入的旧密码错误");
            return returnObject;
        }

        //核对成功
        //将账户名和修改的密码放入map集合
        map.put("loginAct",loginAct);
        map.put("newPwd",newPwd);
        //修改新密码
        int i = userService.updateUserPasswors(map);
        if( i == 1){
            //修改密码成功
            //弹窗显示
            returnObject.setCode(Contans.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setMessage("密码修改成功");
        }
        return returnObject;
    }

}
